package com.bwie.house.config;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.house.entity.ImageEntity;
import com.bwie.house.entity.ResoldEntity;
import com.bwie.house.service.FlatsService;
import com.bwie.house.service.ImageService;
import com.bwie.house.service.ResoldService;
import com.bwie.house.service.UnitService;
import com.bwie.house.vo.FlatsVo;
import com.bwie.house.vo.ResoldVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 龙
 * @version 1.0.0
 * @ClassName SchedEs.java
 * @Description TODO
 * @createTime 2022年04月26日 18:21:00
 */
@Component
public class SchedEs {
    @Autowired
    private FlatsService flatsService;

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private ResoldService resoldService;

    @Autowired
    private ImageService imageService;

    /**
    * @Author: 龍
    * @Description: 楼盘定时启动/30分钟执行一次
    * @DateTime: 2022/4/29 19:13
    * @Params:
    * @Return
    */
    @Scheduled(cron = "0 0/30 * * * *")
    public void flatsSchedEs() throws IOException {

        System.out.println("定时器已启动30分钟查询一次");
        List<FlatsVo> flatsVos = flatsService.flatsvoEs();

        //存入ES
        BulkRequest request = new BulkRequest("flats");
        for (FlatsVo vo : flatsVos) {
            String json = JSON.toJSONString(vo);
            request.add(new IndexRequest().id(vo.getFlatsId().toString())
                    .source(json, XContentType.JSON));
        }
        System.out.println(request);
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
    }

    /**
    * @Author: 龍
    * @Description: 出租房定时启动/30分钟执行一次
    * @DateTime: 2022/4/29 19:13
    * @Params:
    * @Return
    */
    @Scheduled(cron = "0 0/30 * * * *")
    public void rentalSchedEs() throws IOException {

        System.out.println("定时器已启动30分钟查询一次");
        List<FlatsVo> flatsVos = flatsService.flatsvoEs();

        //存入ES
        BulkRequest request = new BulkRequest("flats");
        for (FlatsVo vo : flatsVos) {
            String json = JSON.toJSONString(vo);
            request.add(new IndexRequest().id(vo.getFlatsId().toString())
                    .source(json, XContentType.JSON));
        }
        System.out.println(request);
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
    }


    @Scheduled(cron = "0 0/30 * * * *")
    public void demo2(){
        BulkRequest request = new BulkRequest("resold");
        List<ResoldEntity> resoldEntities = resoldService.list();
        ArrayList<ResoldVo> resoldVos = new ArrayList<>();
        for (ResoldEntity resoldEntity : resoldEntities) {
            ResoldVo resoldVo = new ResoldVo();
            BeanUtils.copyProperties(resoldEntity,resoldVo);
            resoldVos.add(resoldVo);

            for (ResoldVo vo : resoldVos) {
                ArrayList<String> strings = new ArrayList<>();
                QueryWrapper<ImageEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("relation_id",vo.getResoldId());
                wrapper.eq("relation_type",1);
                List<ImageEntity> list = imageService.list(wrapper);
                for (ImageEntity imageEntity : list) {
                    String imageUrl = imageEntity.getImageUrl();
                    strings.add(imageUrl);
                }
                vo.setImages(strings);
            }
            String jsonString = JSON.toJSONString(resoldVo);
            request.add(new IndexRequest().id(resoldVo.getResoldId().toString())
                    .source(jsonString,XContentType.JSON));
        }
        try {
            BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
